﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WebEntities.Models.WebInfo
{
    public class NewsCata
    {
        public int CataID { get; set; }
        public string CataName { get; set; }
        public int? RootCataID { get; set; }
        public string RootCataName { get; set; }
        public int? SortNum { get; set; }
        public int? ParentCataID { get; set; }
        public string ParentCataName { get; set; }
        public string Description { get; set; }
        public int? CataStatus { get; set; }
        public DateTime CreateTime { get; set; }
        public string Creator { get; set; }
        public string CataCover { get; set; }
        public int? MenuItemID { get; set; }
        public string MenuItemName { get; set; }

        public static NewsCata AsNewsCata(WI_NewsCata wicata, string rootCataName, string parentCataName)
        {
            NewsCata newscata = new NewsCata
            {
                CataID = wicata.CataID,
                CataName = wicata.CataName,
                RootCataID = wicata.RootCataID,
                RootCataName = rootCataName,
                SortNum = wicata.SortNum,
                ParentCataID = wicata.ParentCataID,
                ParentCataName = parentCataName,
                Description = wicata.Description,
                CataStatus = wicata.CataStatus,
                CreateTime = wicata.CreateTime,
                Creator = wicata.Creator,
                CataCover = wicata.CataCover,
                MenuItemID = wicata.MenuItemID
            };

            if (newscata.MenuItemID.HasValue)
            {
                using (SugardbEntities ctx = new SugardbEntities())
                {
                    FW_MenuItem mitem = ctx.FW_MenuItem.FirstOrDefault(c => c.ItemID == (newscata.MenuItemID ?? 0));
                    if (mitem != null)
                    {
                        newscata.MenuItemName = mitem.ItemName;
                    }
                }
            }
            return newscata;
        }
    }
}
